package cn.gdcp.lkqapp10;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final int GET_RES = 1001;
    private static final int GET_IMG = 2001;
    private static final String TAG = "MainActivity";
    private Button btnOkHttpGet;
    private Button btnOkHttpGet2;
    private Button btnOkHttpPost;
    private TextView txtResult;
    private ImageView imageView;
    private OkHttpClient okHttpClient;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnOkHttpGet = findViewById(R.id.btn_okhttpget);
        btnOkHttpGet2 = findViewById(R.id.btn_okhttpget2);
        btnOkHttpPost = findViewById(R.id.btn_okhttppost);
        txtResult = findViewById(R.id.txt_result);
        imageView = findViewById(R.id.iv_img);
        okHttpClient = new OkHttpClient();

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == GET_RES) {
                    String content = (String) msg.obj;
                    txtResult.setText(content);
                } else if (msg.what == GET_IMG) {
                    Bitmap bitmap = (Bitmap) msg.obj;
                    imageView.setImageBitmap(bitmap);
                }
                return false;
            }
        });

        btnOkHttpGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://www.baidu.com/";
                String imgurl = "https://i0.hdslb.com/bfs/album/1eab364136f7dc024eac1d663bb843c43c996798.jpg";
                String phoneBaseUrl = "http://ws.webxml.com.cn//WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=15911111111&userID=";
                /*Request request = new Request.Builder().url(url).get().build();*/
                /*Request request = new Request.Builder().url(imgurl).get().build();*/
                Request request = new Request.Builder().url(phoneBaseUrl).get().build();

                final Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure = " + e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        /*Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
                        Message message = Message.obtain();
                        message.what = GET_IMG;
                        message.obj = bitmap;
                        handler.sendMessage(message);*/

                        String content = response.body().string();
                        Log.e(TAG, "onResponse = " + content);
                        Message message = Message.obtain();
                        message.what = GET_RES;
                        message.obj = content;
                        handler.sendMessage(message);
                    }
                });
            }
        });


        btnOkHttpGet2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://www.baidu.com/";
                String imgurl = "https://i0.hdslb.com/bfs/album/1eab364136f7dc024eac1d663bb843c43c996798.jpg";
                final Request request = new Request.Builder().url(url).get().build();
                final Call call = okHttpClient.newCall(request);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Response response = call.execute();
                            String content = response.body().string();
                            Log.e(TAG, "run = " + content);
                            Message message = Message.obtain();
                            message.what = GET_RES;
                            message.obj = content;
                            handler.sendMessage(message);

                            /*Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
                            Message message = Message.obtain();
                            message.what = GET_IMG;
                            message.obj = bitmap;
                            handler.sendMessage(message);*/
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

        btnOkHttpPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneBaseUrl = "http://ws.webxml.com.cn//WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
                RequestBody requestBody = new FormBody.Builder().add("mobileCode","15911111111").add("userID","").build();
                Request request = new Request.Builder().url(phoneBaseUrl).post(requestBody).build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure = " + e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String content = response.body().string();
                        Log.e(TAG, "onResponse = " + content);
                        Message message = Message.obtain();
                        message.what = GET_RES;
                        message.obj = content;
                        handler.sendMessage(message);
                    }
                });
            }
        });

    }
}
